package multiThread;

import java.util.Random;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <b> 描述:</b> ;<br/>
 *
 * @author <b>作者</b>cdxuhongya@jd.com
 *         <b>时间</b>2017/3/13 15:01<br/>
 *         <b>@CopyRight (c) </b>2015-京东智能版权所有<br/>
 */
public class FutureTaskTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 100; i++) {
            Callable<Integer> task = new Task(i);
            FutureTask<Integer> futureTask = new MyFutureTask(task);
            executorService.submit(futureTask);
        }
    }
}

/**
 *
 * 可以用done这个回调函数，在执行完成后执行一些任务
 * */
class MyFutureTask extends FutureTask<Integer>{
    public MyFutureTask(Callable<Integer> callable) {
        super(callable);
    }

    @Override
    protected void done() {
        try {
            System.out.println(get() + "执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

class Task implements Callable<Integer>{
    public Task(Integer id){
        this.id = id;
    }
    private Integer id;

    public Integer call() throws Exception {
        Thread.sleep(new Random(System.currentTimeMillis()).nextInt(10000));
        return id;
    }
}
